﻿#include <iostream>
#include <graphics.h>
#include <random>
#include <string>
#include <Windows.h>
#include <time.h>
using namespace std;


const int ROW = 15;             //行数
const int COL = 20;             //列数
const int NUM = 100;             //雷数
const int slide = 100;          //隐藏加数
const int flag = 1000;          //标记加数
const int PICSIZE = 50;         //图片大小
int map[ROW + 2][COL + 2];      
IMAGE img[13];

void gamestart();
void gameshow();
void gameplay();
void ShowOthers(int r,int c);
bool SetMenu(int r, int c);


int main()
{
    gamestart();
    gameshow();
    gameplay();
    cin.get();
}


void gamestart()
{
    //初始化游戏
    for (int i = 0; i < ROW + 2; i++)
    {
        for (int j = 0; j < COL + 2; j++)
        {
            map[i][j] = 0;
        }
    }

    //埋雷
    srand(time(NULL));
    for (int i = 0; i < NUM;)
    {
        int a = rand() % ROW + 1;
        int b = rand() % COL + 1;
        if (map[a][b] == 0)
        {
            map[a][b] = -1;
            i++;
        }
    }

    //初始化数字
    for (int i = 1; i < ROW + 1; i++)
    {
        for (int j = 1; j < COL + 1; j++)
        {
            if (map[i][j] != -1)
            {
                int num = 0;
                for (int a = i - 1; a < i + 2; a++)
                {
                    for (int b = j - 1; b < j + 2; b++)
                    {
                        if (map[a][b] == -1)
                        {
                            num++;
                        }
                    }
                }
                map[i][j] = num;
            }
        }
    }

    //隐藏（所有+=100）
    for (int i = 1; i < ROW + 1; i++)
    {
        for (int j = 1; j < COL + 1; j++)
        {
            map[i][j] += slide;
        }
    }
}


void gameshow()
{
    //加载图片
    loadimage(&img[0], L"./img/0.png", PICSIZE, PICSIZE);
    loadimage(&img[1], L"./img/1.png", PICSIZE, PICSIZE);
    loadimage(&img[2], L"./img/2.png", PICSIZE, PICSIZE);
    loadimage(&img[3], L"./img/3.png", PICSIZE, PICSIZE);
    loadimage(&img[4], L"./img/4.png", PICSIZE, PICSIZE);
    loadimage(&img[5], L"./img/5.png", PICSIZE, PICSIZE);
    loadimage(&img[6], L"./img/6.png", PICSIZE, PICSIZE);
    loadimage(&img[7], L"./img/7.png", PICSIZE, PICSIZE);
    loadimage(&img[8], L"./img/8.png", PICSIZE, PICSIZE);
    loadimage(&img[9], L"./img/boom.png", PICSIZE, PICSIZE);
    loadimage(&img[10], L"./img/qi.png", PICSIZE, PICSIZE);
    loadimage(&img[11], L"./img/yincang.png", PICSIZE, PICSIZE);
    loadimage(&img[12], L"./img/fail.png", ROW * PICSIZE / 2, COL * PICSIZE / 2);

    //创建图形界面
    initgraph(COL * PICSIZE, ROW * PICSIZE);
    for (int i = 0; i < COL * PICSIZE + 0; i += PICSIZE)
    {
        for (int j = 0; j < ROW * PICSIZE + 0; j += PICSIZE)
        {
            putimage(i, j, &img[11]);
        }
    }
}

void gameplay()
{
    MOUSEMSG msg;
    while (1)
    {
        while (1) 
        {
            msg = GetMouseMsg();
            if (msg.uMsg == WM_LBUTTONDOWN || msg.uMsg == WM_RBUTTONDOWN)
                break;
        }
        int r = 0, c = 0;
        if (msg.uMsg == WM_LBUTTONDOWN)
        {
            r = msg.y / PICSIZE + 1;
            c = msg.x / PICSIZE + 1;
            if (map[r][c] >= slide - 1 && map[r][c] <= slide + 8)
            {
                map[r][c] -= slide;
            }
        }
        else if (msg.uMsg == WM_RBUTTONDOWN)
        {
            r = msg.y / PICSIZE + 1;
            c = msg.x / PICSIZE + 1;
            if (map[r][c] >= slide - 1 && map[r][c] <= slide + 8)
            {
                map[r][c] += flag;
            }
            else if (map[r][c] >= slide + flag - 1 && map[r][c] <= slide + flag + 8)
            {
                map[r][c] -= flag;
            }
        }
        if (!SetMenu(r, c))
            break;

    }
}

void ShowOthers(int r, int c)
{
    for (int i = r - 1; i < r + 2; i++)
    {
        for (int j = c - 1; j < c + 2; j++)
        {
            if (i == r && j == c)
                continue;
            else
            {
                if (map[i][j] - slide != -1)
                {
                    map[i][j] -= slide;
                    SetMenu(i, j);
                }
            }
        }
    }

    
    //if (map[r - 1][c - 1] == 0)
    //    ShowOthers(r - 1, c - 1);
    //if (map[r - 1][c + 1] == 0)
    //    ShowOthers(r - 1, c + 1);
    //if (map[r + 1][c - 1] == 0)
    //    ShowOthers(r + 1, c - 1);
    //if (map[r + 1][c + 1] == 0)
    //    ShowOthers(r + 1, c + 1);
}   

bool SetMenu(int r, int c)
{
    if (map[r][c] == -1)
    {
        putimage((c - 1) * PICSIZE, (r - 1) * PICSIZE, &img[9]);
        Sleep(1000);
        putimage(COL * PICSIZE / 4, ROW * PICSIZE / 4, &img[12]);
        return false;
    }
    if (map[r][c] == 0)
    {
        putimage((c - 1) * PICSIZE, (r - 1) * PICSIZE, &img[0]);
        ShowOthers(r, c);
        return true;
    }
    if (map[r][c] == 1)
    {
        putimage((c - 1) * PICSIZE, (r - 1) * PICSIZE, &img[1]);
        return true;
    }
    if (map[r][c] == 2)
    {
        putimage((c - 1) * PICSIZE, (r - 1) * PICSIZE, &img[2]);
        return true;
    }
    if (map[r][c] == 3)
    {
        putimage((c - 1) * PICSIZE, (r - 1) * PICSIZE, &img[3]);
        return true;
    }
    if (map[r][c] == 4)
    {
        putimage((c - 1) * PICSIZE, (r - 1) * PICSIZE, &img[4]);
        return true;
    }
    if (map[r][c] == 5)
    {
        putimage((c - 1) * PICSIZE, (r - 1) * PICSIZE, &img[5]);
        return true;
    }
    if (map[r][c] == 6)
    {
        putimage((c - 1) * PICSIZE, (r - 1) * PICSIZE, &img[6]);
        return true;
    }
    if (map[r][c] == 7)
    {
        putimage((c - 1) * PICSIZE, (r - 1) * PICSIZE, &img[7]);
        return true;
    }
    if (map[r][c] == 8)
    {
        putimage((c - 1) * PICSIZE, (r - 1) * PICSIZE, &img[8]);
        return true;
    }
    if (map[r][c] >= slide + flag - 1 && map[r][c] <= slide + flag + 8)
    {
        putimage((c - 1) * PICSIZE, (r - 1) * PICSIZE, &img[10]);
        return true;
    }
    if (map[r][c] >= slide - 1 && map[r][c] <= slide + 8)
    {
        putimage((c - 1) * PICSIZE, (r - 1) * PICSIZE, &img[11]);
        return true;
    }
}
